$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'xk/task/list',
        datatype: "json",
        colModel: [

			{ label: '任务ID', name: 'taskId', index: "task_id", width: 45, key: true ,hidden:true},
			{ label: '选课任务名称', name: 'taskName', width: 75 },
            { label: '任务开始时间', name: 'taskStart', width: 75 },
            { label: '任务结束时间', name: 'taskEnd', width: 75 },
            { label: '状态', name: 'status', width: 75, formatter: function(value, options, row){
                    return value === '0' ?
                        '<span class="label label-danger">未发布</span>' :
                        '<span class="label label-success">已发布</span>';
                } },
            { label: '备注', name: 'remark', width: 75 },
            { label: '操作', name: 'status',edittype:"button", formatter: function(cellValue,grid, rows, state){
                    var opt= "";
                    opt +="<button class='btn btn-info' @click=\\\"change(\" + rows.taskId +\")\\\">详细</button>&nbsp;";
                    if(cellValue === '0'){
                        opt +="<button class='btn btn-danger' @click=\"change(" + rows.taskId +")\">发布</button>&nbsp;" ;
                    }else{
                        opt +="<button class='btn btn-danger' @click=\"change(" + rows.taskId +")\">撤销</button>&nbsp;" ;
                    }
                    return opt;
                }
            }
        ],
		viewrecords: true,
        height: '100%',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            taskName: null
        },
        showList: true,
        title:null,
        task:{
            taskId:null,
            taskName:null,
            taskStart:null,
            taskEnd:null,
            status:null,
            remark:null

        }
    },
    mounted: function (){
        this.dateCtlInit();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.task = {taskId:null,
                taskName:null,
                taskStart:null,
                taskEnd:null,
                status:null,
                remark:null};
        },
        update: function () {
            var taskId = getSelectedRow();
            if(taskId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getTask(taskId);
        },
        del: function () {
            var taskIds = getSelectedRows();
            if(taskIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "xk/task/delete",
                    contentType: "application/json",
                    data: JSON.stringify(taskIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            var url = vm.task.taskId == null ? "xk/task/save" : "xk/task/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.task),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getTask: function(taskId){
            $.get(baseURL + "xk/task/info/"+taskId, function(r){
                vm.task = r.task;
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.taskName},
                page:page
            }).trigger("reloadGrid");
        },
        dateCtlInit(){
            //初始化时间输入框
            $('#obj-begin-time').datetimepicker({
                format: 'yyyy-mm-dd',
                minView: 'month',
                autoclose: 1,
                todayHighlight: 1,//今天高亮
                todayBtn: true,
                language:'zh-CN'
            }).on('hide',function () {
                vm.task.taskStart = $('#obj-begin-time').val()
            });
            $('#obj-end-time').datetimepicker({
                format: 'yyyy-mm-dd',
                minView: 'month',
                autoclose: 1,//选择后自动关闭
                todayHighlight: 1,//今天高亮
                todayBtn: true,
                language:'zh-CN'
                //startDate:new Date()  //日历开始时间
            }).on('hide',function () {
                vm.task.taskEnd = $('#obj-end-time').val()
            });
        }
    }
});